﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using ClientLauncher.Src.WPFUtils;
using Game500.ClientServerCommon.Src;

namespace ClientLauncher.Src.ViewModels
{
  public class GameListViewModel : INotifyPropertyChanged
  {
    private readonly ServerConnectionManager myConnectionManager;
    private readonly BaseMapManager myMapManager;
    private readonly string myServerName;
    private bool? myDialogResult = false;

    public GameListViewModel(ServerConnectionManager connectionManager, BaseMapManager mapManager, string lastUsername = "")
    {
      myConnectionManager = connectionManager;
      myMapManager = mapManager;
      Username = lastUsername;

      FrontendInfo info = myConnectionManager.GetServerInfo();
      myServerName = info.ServerName;
      mapManager.AllowMaps(info.AvailableMaps);
      IEnumerable<GameInfoViewModel> games = info.ActiveGames.Where(x => mapManager.AcceptsMap(x.MapId)).Select(x => new GameInfoViewModel(x, mapManager));
      Games = new ObservableCollection<GameInfoViewModel>(games);
    }

    public RelayCommand JoinCommand
    {
      get
      {
        return new RelayCommand(o =>
                                  {
                                    Result = SelectedGame.ServerAddress;
                                    DialogResult = true;
                                  }, o => Username.Length != 0 && SelectedGame != null);
      }
    }

    public RelayCommand CreateCommand
    {
      get
      {
        return new RelayCommand(o =>
                                  {
                                    var view = new MapSelectionView();
                                    var vm = new MapSelectionViewModel(Username, myMapManager);
                                    view.DataContext = vm;
                                    view.ShowDialog();
                                    if (vm.DialogResult == false) return;

                                    Guid guid = vm.SelectedItem.MapId; // new Guid("6086626c-0897-4a99-8d04-4985ef1e2ad1");//myMapManager.DefaultMapGuid;
                                    Result = myConnectionManager.CreateNewGame(vm.Title, guid).ServerAddress;
                                    DialogResult = true;
                                  }, o => Username.Length != 0);
      }
    }

    public string WindowTitle
    {
      get { return myServerName + "'s games"; }
    }

    public GameInfoViewModel SelectedGame { get; set; }

    public ObservableCollection<GameInfoViewModel> Games { get; set; }

    public string Username { get; set; }

    public IPEndPoint Result { get; private set; }

    public bool? DialogResult
    {
      get { return myDialogResult; }
      set
      {
        myDialogResult = value;
        RaisePropertyChanged("DialogResult");
      }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    private void RaisePropertyChanged(string pstrPropertyName)
    {
      if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(pstrPropertyName));
    }
  }
}